﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Strom.Core.Network.Packets
{
    public class LoginRequestResponse : IPacket
    {
        public static byte ID = 2;

        public Graphic.ILoginForm LoginForm { get; set; }

        public LoginRequestResponse(Graphic.ILoginForm loginForm = null)
        {
            LoginForm = loginForm;
        }

        public void Unpack(BaseClient client, Packet packet)
        {
            byte operation = packet.Reader.ReadByte();
            bool success = packet.Reader.ReadBoolean();

            if(LoginForm == null) return; 

            switch (operation)
            {
                case 0:
                    if(success)
                        LoginForm.RegisterSuccess(client);
                    else
                        LoginForm.RegisterFailed(client);
                    return;
                case 1:
                    if(!success)
                        LoginForm.ConnectFailed(client);
                    return;
                case 2:
                    if(!success)
                        LoginForm.ConnectFailed(client);
                    else
                        LoginForm.ConnectSuccess(client, null);
                    return;
            }
           
        }

        /// <summary>
        /// réponse a la demande de login/inscription
        /// </summary>
        /// <param name="client"></param>
        /// <param name="datas">[0] opération type 0 = inscription, 1 = connexion, 2 = connexion editeur [1] success ? </param>
        public void Pack(BaseClient client, params object[] datas)
        {
            var pw = new PacketWriter();
            pw.Write((byte)(datas[0]));
            pw.Write((bool)(datas[1]));

            client.Send(pw.CompilePacket(ID));
        }
    }
}
